<?php
declare (strict_types=1);

namespace app\course\service\classes\admin;

use app\common\logic\category\admin\Category;
use app\common\service\BaseService;
use app\course\logic\classes\admin\ClassesLink as ClassesLinkLogic;
use app\course\logic\live\admin\Live as LiveLogic;
use app\course\logic\video\admin\Video as VideoLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\facade\Request;

/**
 * 班级课程关联接口类-后台
 */
class ClassesLink extends BaseService
{
    /**
     * 列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-29
     *
     * @return   [type]                         [description]
     */
    public function index(int $classesId)
    {
        // 搜索功能
        $data = app(ClassesLinkLogic::class)->getList($classesId, Request::get('title/s'))->bindAttr('course', ['price', 'scribing_price', 'title', 'cover_url', 'course_type_text'])->hidden(['course']);
        // 定义字段
        $keyList = [
            'title'            => ListItem::make('custom')->title('课程'),
            'course_type_text' => ListItem::make()->title('课程类型'),
            'create_time'      => ListItem::make()->title('添加时间'),
        ];

        $searchKey = [
            'title' => FormItem::make('input')->title(false)->placeholder('名称')->maxlength(30),
        ];

        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        // 课程信息
        // 是否已经完结
        $list->is_finished = app(\app\course\logic\base\admin\Course::class)->isFinished($classesId);

        return $list->send();
    }

    /**
     * 创建
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-29
     *
     * @return   [type]                         [description]
     */
    public function create(int $classes_id)
    {
        $data = Request::post();
        $re   = app(ClassesLinkLogic::class)->addClassesLink($classes_id, $data);

        return $this->parseToData($re->toArray(), 1, '添加成功');
    }

    /**
     * 移除
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-29
     */
    public function delete(int $id)
    {
        app(ClassesLinkLogic::class)->deleteClassesLink($id);

        return $this->parseToData([], 1, '删除成功');

    }

    /**
     * 获取课程列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-14
     *
     * @return   [type]                         [description]
     */
    public function getCourseList(int $classes_id)
    {
        $map = [
            ['mhm_id', '=', Request::get('mhm_id', 1)],
            ['solo', '=', 0]
        ];
        Request::has('title', 'get', true) && $map[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        $category = Request::get('category/d', 0);
        $category && $map[] = ['category', 'like', '%,' . $category . ',%'];
        $count = Request::get('count/d', 15);
        $type  = Request::get('type/d', 1);
        $data  = [];
        $ids   = app(ClassesLinkLogic::class)->where('classes_id', $classes_id)->column('course_id');
        $ids && $map[] = ['id', 'not in', $ids];

        switch ($type) {
            case 2:
                $mod = app(LiveLogic::class);
                break;
            // case 3:
            // $mod = app(LiveSmallLogic::class);
            // break;
            case 1:
            default:
                $mod = app(VideoLogic::class);
                break;
        }

        $data = $mod->with(['cover_url'])->where($map)->field(['id', 'title', 'cover', 'scribing_price', 'price'])->paginate($count);

        // 设置表单
        $list        = new PageList();
        $list->datas = $data;
        $list->types = [
            ['id' => 1, 'title' => '点播课'],
            ['id' => 2, 'title' => '大班课'],
            // ['id' => 3, 'title' => '小班课'],
        ];

        return $list->send();

    }

    /**
     * 获取课程分类列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-18
     *
     * @param int $pid [description]
     * @return   [type]                              [description]
     */
    public function getCourseCategory(int $pid)
    {
        $cates = app(Category::class)->getList($pid)->visible(['id', 'title', 'hasChildren'])->toArray();

        return $this->parseToData($cates, 1, '获取成功');
    }

    public function getCourseCategoryCascader()
    {
        $cates = app(Category::class)->cascader();

        return $this->parseToData($cates, 1, '获取成功');
    }
}
